#include "Animation.h"
#include "Timer.h"
//---------------------------------------------------------------------------------------------//
Animation::Animation(D3DVertexUV* vertices,Texture* pTexture, float frameWidth, float frameHeight, unsigned int cant)
:
	 _vertices(vertices),
	 _pTexture(pTexture),
	 _frameWidth(frameWidth),
	 _frameHeight(frameHeight),
	 _cant(cant),
	 _frameNumber(0),
	 _miliSecondsAcum(0)
{}
//---------------------------------------------------------------------------------------------//
void Animation::getCurrentUV(unsigned int frameNumber)
{	 
	//cantidad frames x y
	int framesX = _pTexture->width  / _frameWidth; 
	int framesY = _pTexture->height / _frameHeight;

	int x = frameNumber % framesX; 
	int y = frameNumber / framesX;

	float xPixels = x * _frameWidth;
	float yPixels = y * _frameHeight;
	
	float U = xPixels / _pTexture->width;
	float V = yPixels / _pTexture->height;
	
	float displacedU = (xPixels + _frameWidth) / _pTexture->width;
	float displacedV = (yPixels + _frameWidth) / _pTexture->height;

	_vertices[0].setUV(U,V);
	_vertices[1].setUV(displacedU,V);
	_vertices[2].setUV(U,displacedV);
	_vertices[3].setUV(displacedU,displacedV);
}
//---------------------------------------------------------------------------------------------//
void Animation::animateSprite(float frameRate)
{ 
	_miliSecondsAcum += Timer::GetDT();
	
	if(_miliSecondsAcum >= frameRate)
	{
		_miliSecondsAcum = 0;
		
		if(_frameNumber >= _cant)
			_frameNumber = 0;
		else
			_frameNumber++;
	}

	getCurrentUV(_frameNumber);
}